// 购物车
const box = document.querySelector(".b2")
const cart = window.localStorage.getItem('cart')
const data = JSON.parse(cart)
const int = document.querySelector(".banxin>.a1>.tui")
// const ipt = document.querySelector(".box2>.b1>ul>li>input")
// console.log(ipts);

// 储存数据信息长度
window.localStorage.setItem("number",data.length)



// 退出
int.addEventListener("click",function(){
    if(!window.confirm("您确定要退出吗")) return
    window.location.href="../html/login.html"
})
let num = 0
let nextNum = 0
let number = 1
sty()
function sty() {
    let str = ``
    data.forEach(item => {

        str += `
        <ul>
        <li><input type="checkbox"></li>
                    <li><img src="${item[3]}" alt=""></li>
                    <li>
                        <p class="p1">${item[1]}</p>
                        <p class="p2">亮白精华</p>
                        <p class="p2">规格：套装F（新包装）</p>
                    </li>
                    <li>
                        ${item[6]}
                    </li>
                    <li>
                        <i>-</i>
                        <p>${number}</p>
                        <span data-s="${nextNum}">+</span>
                    </li>
                    <li>
                        ${item[7]}
                    </li>
                    <li><button data-id="${num}">删除</button></li>
                    </ul>
        `
        box.innerHTML = str
        num++
        nextNum++
    });
}



box.addEventListener("click", function (evt) {
    const e = evt || event
    const target = e.target || e.srcElement
    // 删除
    if (target.tagName === "BUTTON") {
        let id
        data.forEach((item, index) => {
            id = target.dataset.id - 0
            if (id === index) {
                // console.log(item);
                data.splice(id, 1)
                window.localStorage.setItem('cart', JSON.stringify(data))
            }


        })
        target.parentElement.parentElement.remove()

    }
    // 增加减少
    if (target.tagName === "SPAN") {
        // 数量
        let li = target.parentElement
        let node = li.children[1]
        let num = node.innerText
        num++
        node.innerText = num
        // 价钱
        let pev = li.previousElementSibling
        let next = li.nextElementSibling
        let price = pev.innerText
        next.innerText = price * num

    }
    if (target.tagName === "I") {
        let li = target.parentElement
        let node = li.children[1]
        let num = node.innerText
        num--
        if (num < 0) return
        node.innerText = num
        let pev = li.previousElementSibling
        let next = li.nextElementSibling
        let price = pev.innerText
        next.innerText = price * num
    }
    if (target.tagName === "INPUT") {
        
    }

})

// 退出

